package com.icode.core.model.house;

import com.icode.core.dto.house.HouseExtraFeeDTO;
import com.icode.core.dto.house.HouseInfoDTO;
import com.icode.core.model.AbstractEntity;
import com.icode.core.shared.ImageUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.*;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: SunKai
 * Date: 14-10-23
 * Time: 下午3:19
 * To change this template use File | Settings | File Templates.
 */
@Entity
@Table(name = "house_info")
public class HouseInfo extends AbstractEntity {

    @Embedded
    private HouseBaseInfo baseInfo;

    @ManyToOne
    @JoinColumn(name = "building_type_id")
    private BuildingType buildingType;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "house_info_id")
    private List<HouseInfoEquipment> equipment;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "house_info_id")
    private List<HouseInfoSupporting> supporting;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "house_info_id")
    private List<HouseInfoImage> houseInfoImages;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "house_info_id")
    private List<HouseWeek> houseWeeks;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "house_info_id")
    private List<HouseExtraFee> houseExtraFees;

    public HouseInfo() {
    }

    public HouseInfo(HouseBaseInfo baseInfo) {
        this.baseInfo = baseInfo;
    }

    public HouseInfo(String name, String houseFeature, String price, String title, UploadImage titleImage, boolean fineRoom) {
        HouseBaseInfo baseInfo = new HouseBaseInfo(name, houseFeature, price, title, titleImage, fineRoom);
        this.baseInfo = baseInfo;
    }

    public HouseBaseInfo baseInfo() {
        return baseInfo;
    }

    public List<HouseInfoEquipment> equipment() {
        return equipment;
    }

    public List<HouseInfoSupporting> supporting() {
        return supporting;
    }

    public List<HouseInfoImage> houseInfoImages() {
        return houseInfoImages;
    }

    public BuildingType buildingType() {
        return buildingType;
    }

    public List<HouseExtraFee> houseExtraFees() {
        return houseExtraFees;
    }

    public List<HouseWeek> houseWeek() {
        return houseWeeks;
    }

    public void initEquipment(List<HouseInfoEquipment> equipments, List<HouseInfoSupporting> supporting, List<HouseInfoImage> houseInfoImages, BuildingType buildingType, List<HouseExtraFee> houseExtraFees) {
        this.equipment = equipments;
        this.supporting = supporting;
        this.houseInfoImages = houseInfoImages;
        this.buildingType = buildingType;
        this.houseExtraFees = houseExtraFees;
    }

    public void initEquipment(List<HouseInfoEquipment> equipments, List<HouseInfoSupporting> supporting) {
        this.equipment = equipments;
        this.supporting = supporting;

    }

    public void update(HouseInfoDTO houseInfoDTO) {
        String name = houseInfoDTO.getName();
        String houseFeature = houseInfoDTO.getHouseFeature();
        String price = houseInfoDTO.getPrice();
        List<MultipartFile> images = houseInfoDTO.getImages();
        String title = houseInfoDTO.getTitle();
        MultipartFile titleImage = houseInfoDTO.getTitleImage();
        List<HouseExtraFeeDTO> extraFeeDTOs = houseInfoDTO.getHouseExtraFeeDTOs();
        initExtraFee(extraFeeDTOs);
        boolean fineRoom = houseInfoDTO.isFineRoom();

        List<UploadImage> uploadImages = ImageUtils.resolveUploadImages(images);
        UploadImage titleUploadImage = null;
        try {
            titleUploadImage = new UploadImage(titleImage.getName(), titleImage.getContentType(), titleImage.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }

        intiIamges(uploadImages);
        this.baseInfo = new HouseBaseInfo(name, houseFeature, price, title, titleUploadImage, fineRoom);
    }

    public void initExtraFee(List<HouseExtraFeeDTO> extraFeeDTOs) {
        List<HouseExtraFee> houseExtraFees = this.houseExtraFees;
        Iterator<HouseExtraFee> iterator = houseExtraFees.iterator();
        int index = 0;
        while (iterator.hasNext()) {
            HouseExtraFee next = iterator.next();
            HouseExtraFeeDTO houseExtraFeeDTO = extraFeeDTOs.get(index);
            next.update(houseExtraFeeDTO);
            index++;
        }
    }

    private void intiIamges(List<UploadImage> uploadImages) {
        List<HouseInfoImage> houseInfoImages = this.houseInfoImages;
        Iterator<HouseInfoImage> iterator = houseInfoImages.iterator();
        int index = 0;
        while (iterator.hasNext()) {
            HouseInfoImage next = iterator.next();
            next.update(uploadImages.get(index));
            index++;
        }
    }

    public void update(String name, String houseFeature, String price, String title, UploadImage titleImage, boolean fineRoom) {
        HouseBaseInfo baseInfo = new HouseBaseInfo(name, houseFeature, price, title, titleImage, fineRoom);
        this.baseInfo = baseInfo;
    }
}
